﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Xpedite.Localization;
using Xpedite.Mvc.Extensions;
using Xpedite.Resources.Definitions;
using Xpedite.Test;

/* Copyright (c) 2010 CBaxter
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), 
 * to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, 
 * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 
 * IN THE SOFTWARE. 
 */

namespace Xpedite.Mvc.Test.Extensions
{
  [TestClass]
  public class ViewDataExtensionsTest
  {
    [TestMethod]
    public void TestStoreDefinitionOnFirstViewCall()
    {
      var htmlHelper = MockHtmlHelperFactory.Create();
      var definition = new Mock<IResourceDefinition>(MockBehavior.Strict);

      definition.SetupGet(mock => mock.ReferenceName).Returns("MyDefinition");

      htmlHelper.StoreDefinition(definition.Object);

      Assert.AreSame(definition.Object, GetResourceDefinitions(htmlHelper)["MyDefinition"]);
    }

    [TestMethod]
    public void TestStoreDefinitionOnSubsequentViewCall()
    {
      var htmlHelper = MockHtmlHelperFactory.Create();
      var definition1 = new Mock<IResourceDefinition>(MockBehavior.Strict);
      var definition2 = new Mock<IResourceDefinition>(MockBehavior.Strict);

      definition1.SetupGet(mock => mock.ReferenceName).Returns("MyDefinition1");
      definition2.SetupGet(mock => mock.ReferenceName).Returns("MyDefinition2");

      htmlHelper.StoreDefinition(definition1.Object);
      htmlHelper.StoreDefinition(definition2.Object);

      var definitions = GetResourceDefinitions(htmlHelper);
      Assert.AreSame(definition1.Object, definitions["MyDefinition1"]);
      Assert.AreSame(definition2.Object, definitions["MyDefinition2"]);
    }

    [TestMethod]
    public void TestStoreDefinitionOnDuplicateReferenceName()
    {
      var htmlHelper = MockHtmlHelperFactory.Create();
      var definition1 = new Mock<IResourceDefinition>(MockBehavior.Strict);
      var definition2 = new Mock<IResourceDefinition>(MockBehavior.Strict);

      definition1.SetupGet(mock => mock.ReferenceName).Returns("MyDefinition1");
      definition2.SetupGet(mock => mock.ReferenceName).Returns("MyDefinition1");

      htmlHelper.StoreDefinition(definition1.Object);

      Expect.Exception(() => htmlHelper.StoreDefinition(definition2.Object), new XpediteException(String.Format(ExceptionMessages.DuplicateDefinitionReferenceName, "MyDefinition1")));
    }

    [TestMethod]
    public void TestRetrieveDefinitionWhenNoDefinitionsDefined()
    {
      var htmlHelper = MockHtmlHelperFactory.Create();

      Expect.Exception(() => htmlHelper.RetrieveDefinition("MyDefinition"), new XpediteException(String.Format(ExceptionMessages.UnknownDefinitionReferenceName, "MyDefinition")));
    }

    [TestMethod]
    public void TestRetrieveDefinitionWhenDefinitionNotDefined()
    {
      var htmlHelper = MockHtmlHelperFactory.Create();
      var definition = new Mock<IResourceDefinition>(MockBehavior.Strict);

      definition.SetupGet(mock => mock.ReferenceName).Returns("MyDefinition");

      htmlHelper.StoreDefinition(definition.Object);

      Expect.Exception(() => htmlHelper.RetrieveDefinition("AnotherDefinition"), new XpediteException(String.Format(ExceptionMessages.UnknownDefinitionReferenceName, "AnotherDefinition")));
    }

    [TestMethod]
    public void TestRetrieveDefinitionWhenDefinitionDefined()
    {
      var htmlHelper = MockHtmlHelperFactory.Create();
      var definition = new Mock<IResourceDefinition>(MockBehavior.Strict);

      definition.SetupGet(mock => mock.ReferenceName).Returns("MyDefinition");

      htmlHelper.StoreDefinition(definition.Object);

      Assert.AreSame(definition.Object, htmlHelper.RetrieveDefinition("MyDefinition"));
    }

    private static IDictionary<String, IResourceDefinition> GetResourceDefinitions(HtmlHelper htmlHelper)
    {
      return (IDictionary<String, IResourceDefinition>)htmlHelper.ViewContext.ViewData[typeof(ViewDataExtensions).AssemblyQualifiedName + "(ResourceDefinitions)"];
    }
  }
}
